<?php

class UsersController extends AppController {
    
    var $name = 'Users';
    var $roles = array();
    var $index;
    
    // paginado de grilla
    public $components = array('Paginator');
    public $paginate = null;
    
    
    public function beforeFilter() {
        parent::beforeFilter();
        // Allow users to register and logout.
        $this->Auth->allow('add', 'logout');
    }
    
    public function index() {
        
        $this->set('grafico', $this->data);
        $grafico = $this->data;
        
        if (!empty($this->data)) {
            $condiciones = array();
            if (!empty($this->data['User']['filtro_usuario'])) {
                $condiciones['User.username'] = $this->data['User']['filtro_usuario'];
            }
            if (!empty($this->data['User']['filtro_nombre'])) {
                $condiciones['User.nombre LIKE'] = '%'.$this->data['User']['filtro_nombre'].'%';
            }
            if (!empty($this->data['User']['filtro_apellido'])) {
                $condiciones['User.apellido LIKE'] = '%'.$this->data['User']['filtro_apellido'].'%';
            }
            if (!empty($this->data['User']['filtro_dni'])) {
                $condiciones['User.dni LIKE'] = '%'.$this->data['User']['filtro_dni'].'%';
            }
            if (!empty($this->data['User']['filtro_cargo'])) {
                $condiciones['User.cargo LIKE'] = '%'.$this->data['User']['filtro_cargo'].'%';
            }
            
            
            $condiciones['User.activo'] = $this->data['User']['filtro_activo'];
            $this->paginate = array(
                    'limit' => 11,
                    'order' => array('User.codigo' => 'asc', 'User.descripcion' => 'asc'),
                    'conditions' => $condiciones);            
        } else {
            $condiciones = array('User.activo' => 1);
            $this->paginate = array(
                    'limit' => 11,
                    'order' => array('User.username' => 'asc','User.nombre' => 'asc'),
                    'conditions' => $condiciones);
        }
        
        $this->Paginator->settings = $this->paginate;    
        $data = $this->Paginator->paginate('User');
        
        $this->set('User', $data);               
        $this->data = $this->User->create(); 
        
        
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    public function view($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->set('user', $this->User->read(null, $id));     
    }
        

    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        }
    }

    public function edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
    }

    public function delete($id = null) {
        $this->request->onlyAllow('post');

        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->User->delete()) {
            $this->Session->setFlash(__('User deleted'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not deleted'));
        return $this->redirect(array('action' => 'index'));
    }
    
    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirect());
            }
            $this->Session->setFlash(__('El usuario o la contraseña son incorrectos.'));
        }
    }

    public function logout() {
        return $this->redirect($this->Auth->logout());
    }    
    
     function editar($id = null) {
        Debugger::dump($id);
        if (empty($this->request->data)) {
            $this->request->data = $this->User->findById($id);
            
            //$index = $this->request->data['User']['cargo'];
            //$this->request->data['User']['cargo'] = $index -1;
            
        } else {
            // Save logic goes here
            if (!empty($this->data)) {
                $this->User->id = $id;
                
               // $roles = array('1','2','3','4');
               // $index = $this->request->data['User']['cargo'];
               // $this->request->data['User']['cargo'] = $roles[$index];
                
                if ($this->User->save($this->data)) {
                    $this->Session->setFlash('El usuario fue actualizado.');
                    $this->redirect(array('action' => 'index'));
                }
            }
        }
    }
    
    function nuevo() {
        if (!empty($this->data)) {
            
            //$roles = array('1','2','3','4');
            //$index = $this->request->data['User']['cargo'];
            //$this->request->data['User']['cargo'] = $roles[$index];
            
            $this->User->create();
            if ($this->User->save($this->data)) {
                $this->Session->setFlash('El usuario fue agregado.');
                $this->redirect(array('action' => 'index'));
                //return 'saved'; 
            }
        }
    }
    
}    
    
?>
